package io.renren.modules.courseTest.service.impl;

import io.renren.common.utils.ReadExcel;
import io.renren.modules.courseTest.dao.courseTestDao;
import io.renren.modules.courseTest.entity.ChooseTestCommentEntity;
import io.renren.modules.courseTest.entity.ChooseTestEntity;
import io.renren.modules.courseTest.entity.QuestionOption;
import io.renren.modules.courseTest.entity.UserChooseTestInfo;
import io.renren.modules.courseTest.service.courseTestService;
import io.renren.modules.sys.dao.TokenDao;
import io.renren.modules.sys.dao.UserDao;
import io.renren.modules.sys.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class courseTestServiceImpl implements courseTestService {


    @Autowired
    courseTestDao courseTestDao;

    @Autowired
    TokenDao tokenDao;

    @Autowired
    UserDao userDao;


    @Override
    public List getCourseTestIdList(Map map) {
        List ids = courseTestDao.getCourseTestIdList(map);

        return ids;
    }

    @Override
    public ChooseTestEntity getOneCourseTest(int id) {
        ChooseTestEntity courseTestEntity = courseTestDao.getOneCourseTest(id);
        List list = new ArrayList();
        QuestionOption questionOption = new QuestionOption();
        if(courseTestEntity.getOptionOne()!= null && !"无".equals(courseTestEntity.getOptionOne())){
            questionOption.setOption(courseTestEntity.getOptionOne());
        }else {
            questionOption.setOption("");
        }
        if(courseTestEntity.getOptionOneImg()!= null && !"无".equals(courseTestEntity.getOptionOneImg())){
            questionOption.setImg(courseTestEntity.getOptionOneImg());
        }else{
            questionOption.setImg("");
        }
       list.add(questionOption);
        QuestionOption questionOption1 = new QuestionOption();
        if(courseTestEntity.getOptionTwo()!= null && !"无".equals(courseTestEntity.getOptionTwo())){
            questionOption1.setOption(courseTestEntity.getOptionTwo());
        }else{
            questionOption1.setOption("");
        }
        if(courseTestEntity.getOptionTwoImg()!= null  && !"无".equals(courseTestEntity.getOptionTwoImg())){
            questionOption1.setImg(courseTestEntity.getOptionTwoImg());
        }else{
            questionOption1.setImg("");
        }
       list.add(questionOption1);
        QuestionOption questionOption2 = new QuestionOption();
        if(courseTestEntity.getOptionThree()!= null && !"无".equals(courseTestEntity.getOptionThree())){
            questionOption2.setOption(courseTestEntity.getOptionThree());
        }else{
            questionOption2.setOption("");
        }
        if(courseTestEntity.getOptionThreeImg()!= null  && !"无".equals(courseTestEntity.getOptionThreeImg())){
            questionOption2.setImg(courseTestEntity.getOptionThreeImg());
        }else{
            questionOption2.setImg("");
        }
        list.add(questionOption2);
        QuestionOption questionOption3 = new QuestionOption();
        if(courseTestEntity.getOptionFour()!= null  && !"无".equals(courseTestEntity.getOptionFour())){
            questionOption3.setOption(courseTestEntity.getOptionFour());
        }else{
            questionOption3.setOption("");
        }
        if(courseTestEntity.getOptionFourImg()!= null && !"无".equals(courseTestEntity.getOptionFourImg())){
            questionOption3.setImg(courseTestEntity.getOptionFourImg());
        }else{
            questionOption3.setImg("");
        }
       list.add(questionOption3);

        /*list.add(courseTestEntity.getOptionOne());
        list.add(courseTestEntity.getOptionTwo());
        list.add(courseTestEntity.getOptionThree());
        list.add(courseTestEntity.getOptionFour());*/
        courseTestEntity.setQuestionOptions(list);
        return courseTestEntity;
    }

    @Override
    public String getTestAnswer(int id) {
        return courseTestDao.getTestAnswer(id);
    }

    @Override
    public int getFraction(int id) {
        return courseTestDao.getFraction(id);
    }

    @Override
    public List getAllCourseTest(Map map) {
        List allTestList = new ArrayList();
        List ids = courseTestDao.getCourseTestIdList(map);
        for(int i = 0;i<ids.size();i++){
            ChooseTestEntity courseTestEntity = getOneCourseTest(Integer.parseInt(ids.get(i).toString()));
           /* List list = new ArrayList();
            list.add(courseTestEntity.getOptionOne());
            list.add(courseTestEntity.getOptionTwo());
            list.add(courseTestEntity.getOptionThree());
            list.add(courseTestEntity.getOptionFour());
            courseTestEntity.setQuestionOptions(list);*/
            allTestList.add(courseTestEntity);
        }
        return allTestList;
    }

    @Override
    public void insertCourseTestComment(ChooseTestCommentEntity chooseTestCommentEntity) {
        int userId =Integer.valueOf(tokenDao.getUserIdByToken(chooseTestCommentEntity.getToken()).toString());
        UserEntity userEntity =  userDao.getUserByUserId(Long.valueOf(userId));

        chooseTestCommentEntity.setUserId(userId);
        chooseTestCommentEntity.setHeadImg(userEntity.getHeadImg());
        chooseTestCommentEntity.setContentB(chooseTestCommentEntity.getContent().getBytes());
        chooseTestCommentEntity.setContent(null);
        chooseTestCommentEntity.setNickNameB(userEntity.getNickNameB());
        chooseTestCommentEntity.setNickName(null);
        courseTestDao.insertCourseTestComment(chooseTestCommentEntity);
    }

    @Override
    public void deleteCourseTestComment(Map map) {
        courseTestDao.deleteCourseTestComment(map);
    }

    @Override
    public List getCourseTestComment(Map params) {
        List list = courseTestDao.getCourseTestComment(params);
        for(int i = 0;i<list.size();i++){
            ChooseTestCommentEntity chooseTestCommentEntity = (ChooseTestCommentEntity)list.get(i);
            chooseTestCommentEntity.setNickName(new String(chooseTestCommentEntity.getNickNameB()));
            chooseTestCommentEntity.setContent(new String(chooseTestCommentEntity.getContentB()));
            chooseTestCommentEntity.setNickNameB(null);
            chooseTestCommentEntity.setContentB(null);
        }
        return list;
    }

    @Override
    public  List readExcelFile(MultipartFile file) {
        ReadExcel readExcel = new ReadExcel();
        try {
            List result = readExcel.readExcel(file);
            /*for(int i=0;i<result.size();i++){
                System.out.println(result.get(i).toString());
            }*/
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addUserChooseTestInfo(UserChooseTestInfo userChooseTestInfo) {
        courseTestDao.addUserChooseTestInfo(userChooseTestInfo);
    }

    @Override
    public void updateUserChooseTestInfo(UserChooseTestInfo userChooseTestInfo) {
        courseTestDao.updateUserChooseTestInfo(userChooseTestInfo);
    }

    @Override
    public UserChooseTestInfo getUserChooseTestInfo(Map map) {
        return courseTestDao.getUserChooseTestInfo(map);
    }
}
